<?php
include("../../inc/global.php");
include("../menusys/function.php");
adminislogin();
$UserID=$_GET["UserID"];
function isEmpty($isEmpty_String){
	if(empty($isEmpty_String)){
		return 0;
	}else{
		return $isEmpty_String;
	}
}


//echo isGetRule(2,$_SESSION["admin"][0],1);

//保存权限
if(strtolower($_SERVER['REQUEST_METHOD'])=="post"){
		$result=$db->query("select * from menusys where menusys2<>0 order by menusys3 Asc");
		while($vRow=$db->getarray($result)){
			$r=0;
			for($i=1;$i<=5;$i++){
				if(isset($_POST["ID{$vRow[0]}{$i}"])){
					$r+=$_POST["ID{$vRow[0]}{$i}"];
				}
			}
			
			$MenuRule="";
			$RuleGroup=$vRow[7];
			if(!empty($RuleGroup)){
				if(strpos($RuleGroup,",")===false){
					$MenuRule_arr=explode("|",$RuleGroup);
					if($MenuRule_arr[0]==$UserID){
						$MenuRule="{$UserID}|{$r}";
					}else{
						$MenuRule="{$RuleGroup},{$UserID}|{$r}";
					}
				}else{
					$RuleGroup_arr=explode(",",$RuleGroup);
					$istype='0';
					for($j=0;$j<count($RuleGroup_arr);$j++){
						$MenuRule_arr=explode("|",$RuleGroup_arr[$j]);
						if($MenuRule_arr[0]==$UserID){
							$MenuRule.="{$UserID}|{$r}";
							$istype='1';
						}else{
							$MenuRule.="{$RuleGroup_arr[$j]}";
						}
						if($j<count($RuleGroup_arr)-1){
							$MenuRule.=",";
						}
					}
					
					if($istype=='0'){
						$MenuRule.=",{$UserID}|{$r}";
					}
				}
			}else{
				$MenuRule="{$UserID}|{$r}";
			}
			$SQLstring="UPdate menusys Set menusys7='{$MenuRule}' where menusys0={$vRow[0]}";
			//echo $SQLstring."<br>";
			$db->exec($SQLstring);
		}
}



echo "<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>权限管理</title>
<link href=\"../../Css.css\" rel=\"stylesheet\" type=\"text/css\" />
<script language=\"JavaScript\" type=\"text/javascript\">
function CheckAll()
  {
  for (var i=0;i<document.form1.elements.length;i++)
    {
    var e = document.form1.elements[i];
    if (e.Name != \"chkAll\")
       e.checked = document.all.chkAll.checked;
    }
  }
</script>
</head>

<body>
<form id=\"form1\" name=\"form1\" method=\"post\" action=\"?UserID={$UserID}\">

<table width=\"80%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\">";

$Data_result=$db->query("Select * from menusys where menusys2=0 Order by menusys3 Asc");
while($DataRow=$db->getarray($Data_result)){

echo "<tr>
	    <td height=\"26\"  class=\"TdLineBG\">{$DataRow[1]}</td>
	  </tr>
	  <tr>
	    <td height=\"26\">
			<table width=\"90%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\">";

			  $Data1_result=$db->query("select * from menusys where menusys2={$DataRow[0]} order by menusys3 Asc");
			  while($DataRow1=$db->getarray($Data1_result)){
			  	
			  	$MenuRule="";
			  	$RuleGroup=$DataRow1[7];
			  	if(!empty($RuleGroup)){
					if(strpos($RuleGroup,",")===false){
						$MenuRule_arr=explode("|",$RuleGroup);
						if($MenuRule_arr[0]==$UserID){
							$MenuRule=$RuleGroup;
						}
					}else{
						$RuleGroup_arr=explode(",",$RuleGroup);
						for($j=0;$j<count($RuleGroup_arr);$j++){
							$MenuRule_arr=explode("|",$RuleGroup_arr[$j]);
							if($MenuRule_arr[0]==$UserID){
								$MenuRule=$RuleGroup_arr[$j];
							}
						}
					}
				}else{
					$MenuRule="{$UserID}|0";
				}
				
				$ThisMenuRules_arr=explode("|",$MenuRule);
				$temp_ThisMenuRules=isset($ThisMenuRules_arr[1])?$ThisMenuRules_arr[1]:'';;
				$ThisMenuRules=GetRule($temp_ThisMenuRules,5);
				$checkstring="";
				
				for($i=0;$i<5;$i++){
					if(strval(substr($ThisMenuRules,$i,1))=="1"){
						$checkstring[$i]=" checked=\"checkbox\" ";
					}else{
						$checkstring[$i]=" ";
					}
				}
				
			echo "<tr>
				    <td width=\"100\" height=\"26\" class=\"TdLine\">{$DataRow1[1]}</td>
				    <td width=\"100\" height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow1[0]}1\" value=\"1\" {$checkstring[0]} />浏览</td>
				    <td width=\"100\" height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow1[0]}2\" value=\"2\" {$checkstring[1]} />添加</td>
				    <td width=\"100\" height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow1[0]}3\" value=\"4\" {$checkstring[2]} />修改</td>
				    <td width=\"100\" height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow1[0]}4\" value=\"8\" {$checkstring[3]} />删除</td>
				    <td height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow1[0]}5\" value=\"16\" {$checkstring[4]} />审核</td>
				  </tr>";
				  
				  //三级
				  echo "<tr>
				  		<td colspan=\"6\"><table width=\"90%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\">
				    ";
				  $Data2_result=$db->query("select * from menusys where menusys2={$DataRow1[0]} and menusys9=0 order by menusys3 Asc");
				   while($DataRow2=$db->getarray($Data2_result)){
				   	  
					  		$MenuRule="";
							$RuleGroup=$DataRow2[7];
							if(!empty($RuleGroup)){
								if(strpos($RuleGroup,",")===false){
									$MenuRule_arr=explode("|",$RuleGroup);
									if($MenuRule_arr[0]==$UserID){
										$MenuRule=$RuleGroup;
									}
								}else{
									$RuleGroup_arr=explode(",",$RuleGroup);
									for($j=0;$j<count($RuleGroup_arr);$j++){
										$MenuRule_arr=explode("|",$RuleGroup_arr[$j]);
										if($MenuRule_arr[0]==$UserID){
											$MenuRule=$RuleGroup_arr[$j];
										}
									}
								}
							}else{
								$MenuRule="{$UserID}|0";
							}
							
							$ThisMenuRules_arr=explode("|",$MenuRule);
							$ThisMenuRules=GetRule($ThisMenuRules_arr[1],5);
							$checkstring="";
							
							for($i=0;$i<5;$i++){
								if(strval(substr($ThisMenuRules,$i,1))=="1"){
									$checkstring[$i]=" checked=\"checkbox\" ";
								}else{
									$checkstring[$i]=" ";
								}
							}
							
						echo "<tr>
								<td width=\"100\" height=\"26\" class=\"TdLine\">{$DataRow2[1]}</td>
								<td width=\"100\" height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow2[0]}1\" value=\"1\" {$checkstring[0]} />浏览</td>
								<td width=\"100\" height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow2[0]}2\" value=\"2\" {$checkstring[1]} />添加</td>
								<td width=\"100\" height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow2[0]}3\" value=\"4\" {$checkstring[2]} />修改</td>
								<td width=\"100\" height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow2[0]}4\" value=\"8\" {$checkstring[3]} />删除</td>
								<td height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow2[0]}5\" value=\"16\" {$checkstring[4]} />审核</td>
							  </tr>";
							  
							  
							  
								  //四级
								  echo "<tr>
										<td colspan=\"6\"><table width=\"90%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\">
									";
								  $Data3_result=$db->query("select * from menusys where menusys2={$DataRow2[0]} and menusys9=0 order by menusys3 Asc");
								   while($DataRow3=$db->getarray($Data3_result)){
									  
											$MenuRule="";
											$RuleGroup=$DataRow3[7];
											if(!empty($RuleGroup)){
												if(strpos($RuleGroup,",")===false){
													$MenuRule_arr=explode("|",$RuleGroup);
													if($MenuRule_arr[0]==$UserID){
														$MenuRule=$RuleGroup;
													}
												}else{
													$RuleGroup_arr=explode(",",$RuleGroup);
													for($j=0;$j<count($RuleGroup_arr);$j++){
														$MenuRule_arr=explode("|",$RuleGroup_arr[$j]);
														if($MenuRule_arr[0]==$UserID){
															$MenuRule=$RuleGroup_arr[$j];
														}
													}
												}
											}else{
												$MenuRule="{$UserID}|0";
											}
											
											$ThisMenuRules_arr=explode("|",$MenuRule);
											$ThisMenuRules=GetRule($ThisMenuRules_arr[1],5);
											$checkstring="";
											
											for($i=0;$i<5;$i++){
												if(strval(substr($ThisMenuRules,$i,1))=="1"){
													$checkstring[$i]=" checked=\"checkbox\" ";
												}else{
													$checkstring[$i]=" ";
												}
											}
											
										echo "<tr>
												<td width=\"100\" height=\"26\" class=\"TdLine\">{$DataRow3[1]}</td>
												<td width=\"100\" height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow3[0]}1\" value=\"1\" {$checkstring[0]} />浏览</td>
												<td width=\"100\" height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow3[0]}2\" value=\"2\" {$checkstring[1]} />添加</td>
												<td width=\"100\" height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow3[0]}3\" value=\"4\" {$checkstring[2]} />修改</td>
												<td width=\"100\" height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow3[0]}4\" value=\"8\" {$checkstring[3]} />删除</td>
												<td height=\"26\" class=\"TdLine\"><input type=\"checkbox\" name=\"ID{$DataRow3[0]}5\" value=\"16\" {$checkstring[4]} />审核</td>
											  </tr>";
											  
  
											  
											  
								   }
								   
								   echo "</table></td></tr>";
							  

							  
				   }
				   
				   echo "</table></td></tr>";
				   
				  
				   
				  
				  
				  
			  }
			echo "</table>
	    </td>
	  </tr>";
}
echo "</table>

<label>
<div align=\"center\"><br><input name=\"chkAll\" type=\"checkbox\" id=\"chkAll\" onClick=\"CheckAll()\" value=\"checkbox\" />全选
  <input type=\"submit\" name=\"Submit\" value=\" 保存 \">
  <input type=\"button\" name=\"Submit2\" value=\" 取消 \" onClick=\"window.history.back();\"/>
</div>
</label>

</form>
</body>
</html>";

?>
